import cv2 as cv
import numpy
import os

#创建一个120000字节的随机数组
randomByteArray = bytearray(os.urandom(120000))#os.urandom()函数可随机生成原始字节随后会把该字节转换为NumPy数组
# numpy.random.randint(0,256,120000).reshape(300,400)#更高效生成NumPy数组
flatNumpyArray = numpy.array(randomByteArray)

#把数组创建成一张400x300的图片
grayImage = flatNumpyArray.reshape(300,400)
bgrImage = flatNumpyArray.reshape(100,400,3)
test = flatNumpyArray.reshape(300,400,-1)
#创建窗口并显示图像
cv.namedWindow("image")
cv.namedWindow("image2")
cv.namedWindow("test")
cv.imshow("image",grayImage)
cv.imshow("image2",bgrImage)
cv.imshow("image2",test)
cv.waitKey(0)
cv.imwrite('test1.png',test)
#释放窗口
cv.destroyAllWindows()
